<?php

namespace App\Http\ControllersVer1;

use Illuminate\Http\Request;

use Qcloud\Sms\SmsSingleSender;
use Illuminate\Support\Facades\Redis;

class SmsController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //
    public function test() {

        $redis = Redis::connection();
        $foo = $redis->get('foo');


        return response()->json([
            'code' => 2000,
            'foo' => $foo,
            'msg' => 'api v1 test success!'
        ]);

    }


    public function sendSms(Request $request) {

        $this->validate($request, [
            'mobile' => 'required|digits:11',
            'templ' => 'required'
        ]);

        $mobile = $request->input('mobile');
        $templ = $request->input('templ');

        $templates = [
            'veri_code' => 12045,
            'other' => 12345
        ];

        $templ_id = $templates[$templ];
        $veri_code = '987543';
        $params = [$veri_code];

        //
        $app_id = 1400025900;
        $app_key = "d487fe19f2246f732187c8e158b0017f";
        $singleSender = new SmsSingleSender($app_id, $app_key);
        $result = $singleSender->sendWithParam("86", $mobile, $templ_id, $params, "", "", "");
        $rsp = json_decode($result);

        return response()->json([
            'code' => 2000,
            'rsp' => $rsp,
            'msg' => 'success!'
        ]);

    }



}
